package com.chinameyer.workorders.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chinameyer.workorders.bean.MeyerCatalogue;
import com.chinameyer.workorders.bean.MeyerStaff;
import com.chinameyer.workorders.dto.staff.AddStaffDTO;
import com.chinameyer.workorders.dto.staff.EditStaffDTO;
import com.chinameyer.workorders.mapper.StaffMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author meyer@HongYe
 */
@Service
public class StaffService {

    @Resource
    private StaffMapper staffMapper;


    public boolean checkStaffIsExist(AddStaffDTO addStaffDTO) {
        QueryWrapper<MeyerStaff> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(MeyerStaff::getCatalogueId,addStaffDTO.getCatalogueId())
                .eq(MeyerStaff::getStaffName,addStaffDTO.getStaffName());
        return this.staffMapper.selectCount(wrapper) > 0;
    }

    @Transactional
    public void addStaff(AddStaffDTO addStaffDTO) {
        MeyerStaff staff = new MeyerStaff();
        BeanUtils.copyProperties(addStaffDTO,staff);
        this.staffMapper.insert(staff);
    }

    @Transactional
    public void deleteStaff(Integer staffId) {
        QueryWrapper<MeyerStaff> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(MeyerStaff::getId,staffId);
        this.staffMapper.delete(wrapper);
    }

    public boolean checkStaffIsExistExpectItself(EditStaffDTO editStaffDTO) {
        QueryWrapper<MeyerStaff> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(MeyerStaff::getCatalogueId,editStaffDTO.getCatalogueId())
                .eq(MeyerStaff::getStaffName,editStaffDTO.getStaffName())
                .ne(MeyerStaff::getId,editStaffDTO.getId());
        return this.staffMapper.selectCount(wrapper) > 0;
    }

    @Transactional
    public void updateStaff(EditStaffDTO editStaffDTO) {
        MeyerStaff staff = new MeyerStaff();
        BeanUtils.copyProperties(editStaffDTO,staff);
        this.staffMapper.updateById(staff);
    }

    public int getAllStaffTotal(Integer catalogueId) {
        QueryWrapper<MeyerStaff> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(MeyerStaff::getCatalogueId,catalogueId);
        return this.staffMapper.selectCount(wrapper);
    }

    public List<MeyerStaff> getAllStaff(Integer catalogueId, int count, int page) {
        int start = count * page;
        int limit = count;
        return this.staffMapper.selectAllStaffWithPage(catalogueId,start,limit);
    }
}
